using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using User.Models;
using User.Service;
using UserLogicCore.Logic;
using UserSorce.Entity.Models;

namespace User.Controllers
{
    public class InventoryController : Controller
    {
        InventoryServiceModels _invnentoryModel = new InventoryServiceModels();
          InventoryService _intenoryctx = new InventoryService();
        private UserService _ctx = new UserService();
        TypeServiceModels _typeService = new TypeServiceModels();
        /// <summary>
        /// 添加清单
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeId"></param>
        /// <param name="inventoryName"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddInventory(int userId,int typeId,string inventoryName)
        {
            var type = GetType(userId, typeId);
            if (inventoryName == null)
            {
                ViewData["Message"] = "输入内容不能为空";
            }
            else
            {
            _intenoryctx.AddInventory(userId, typeId, inventoryName);
             type = GetType(userId, typeId);
            }

            return View("Inventory", type);
        }
        /// <summary>
        /// 查看清单
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowInventory(int userId, int typeId)
        {
            var type = GetType(userId,typeId);
            return View("Inventory", type);
        }
        /// <summary>
        /// 修改清单名称
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeId"></param>
        /// <param name="inventory"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ChangeInventory(int userId,int typeId,int inventoryId,string inventoryName)
        {
             _intenoryctx.ModifyInventoryName(userId, typeId, inventoryId, inventoryName);
            var type = GetType(userId, typeId);
                return View("Inventory", type);   
        }
        /// <summary>
        /// 编辑清单，转换成得到修改清单的信息
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeId"></param>
        /// <param name="inventoryId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult EditInventory(int userId,int typeId,int inventoryId)
        {
            Users userData = _ctx.GetData(userId);
            var inventory = _invnentoryModel.TurnOffInventoryData(userData, typeId, inventoryId);
            return View("EditInventory", inventory);
        }
        /// <summary>
        /// 得到需要查看清单的type
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public  TypesModels GetType(int userId,int typeId)
        {
            Users userData = _ctx.GetData(userId);
            var type = _typeService.TurnOffTypeData(userData, typeId);
            return type;
        }
    }
}